package log

import (
	"github.com/cloudwego/hertz/pkg/common/hlog"
	hertzzap "github.com/hertz-contrib/logger/zap"
	"log"
	"os"
	"path"
	"time"
)

func NewZapLog(config *Config) (logger *hertzzap.Logger) {
	if err := os.MkdirAll(config.path, 0o777); err != nil {
		log.Println(err.Error())
		return
	}

	// 将文件名设置为日期
	logFileName := time.Now().Format("2006-01-02") + ".log"
	fileName := path.Join(config.path, logFileName)
	if _, err := os.Stat(fileName); err != nil {
		if _, err := os.Create(fileName); err != nil {
			log.Println(err.Error())
			return
		}
	}

	logger = hertzzap.NewLogger()
	logger.SetOutput(config.lumberjackLogger)
	logger.SetLevel(hlog.LevelDebug)

	return
}
